package com.wteam.dragon.model.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.model.mapper.DeptDtoMapper;
import com.wteam.dragon.model.mapper.DeptMapper;
import com.wteam.dragon.model.pojo.bean.Dept;
import com.wteam.dragon.model.pojo.dto.DeptDto;
import com.wteam.dragon.model.service.DeptService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.StringUtils;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 陈晓辉
 * @Date 2020/4/2 9:38
 */
@Service
@RequiredArgsConstructor
public class DeptServiceImpl implements DeptService {
    private final DeptMapper deptMapper;
    private final DeptDtoMapper deptDtoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addDept(Dept dept) {
        deptMapper.insert(dept);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delDept(Long deptId) {
         ValidationUtil.isNull("deptId", deptId);
         deptMapper.deleteById(deptId);
        ResultMessage resultMessage = new ResultMessage();
         resultMessage.setStatus(HttpStatus.OK.value());
         resultMessage.setMessage("删除成功");
         return resultMessage;
     }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editDept(Dept dept) {
        deptMapper.updateById(dept);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更新成功");
        return resultMessage;
    }

    @Override
    public ResultMessage treeDept(String deptName) {
        ValidationUtil.isNull("deptName", deptName);
        List<DeptDto> deptTree = null;
        QueryWrapper<DeptDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(DeptDto::getSort);
        if(!StringUtils.isBlank(deptName)){
            queryWrapper.lambda().like(DeptDto::getName, deptName);
            deptTree = deptDtoMapper.selectList(queryWrapper);
            Dict resultMap = Dict.create()
                    .set("message", "请求成功")
                    .set("deptTree",deptTree);
            ResultMessage resultMessage = new ResultMessage();
            resultMessage.setStatus(HttpStatus.OK.value());
            resultMessage.setResultParam(resultMap);
            return resultMessage;
        }
        queryWrapper.lambda().eq(DeptDto::getPid, 0);
        List<DeptDto> deptDtoList = new ArrayList<>(deptDtoMapper.selectList(queryWrapper));
        deptTree = getDeptTree(deptDtoList);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("deptTree",deptTree);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    private List<DeptDto> getDeptTree(List<DeptDto> deptList){
        LinkedHashSet<DeptDto> linkedHashSet = new LinkedHashSet<>();
        deptList.forEach(deptDto -> {
            if(deptDto != null){
                QueryWrapper<DeptDto> deptDtoQueryWrapper = new QueryWrapper<>();
                deptDtoQueryWrapper.lambda()
                        .orderByAsc(DeptDto::getSort)
                        .eq(DeptDto::getPid, deptDto.getId());
                List<DeptDto> deptDtoList = deptDtoMapper.selectList(deptDtoQueryWrapper);
                if(deptDtoList.size() != 0){
                    deptDto.setChildren(deptDtoList);
                    getDeptTree(deptDtoList);
                }
                linkedHashSet.add(deptDto);
            }
        });
        return new ArrayList<>(linkedHashSet);
    }

    @Override
    public ResultMessage listDeptName() {
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        List<Dept> deptList = deptMapper.selectList(queryWrapper);
        List<String> deptNameList = deptList.stream().map(Dept::getName).collect(Collectors.toList());
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("deptNameList",deptNameList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
